﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SixApp.Entities;
using SixApp.Extensions;
using SixApp.Repositories;

namespace SixApp
{
    public partial class FormHistory : BaseForm
    {
        private HistoryRepository mHistoryRepository = new HistoryRepository();

        public DataGridViewRow HistoryCurrentRow
        {
            get
            {
                return gridHistory.CurrentRow;
            }
        }

        public FormHistory()
        {
            InitializeComponent();
            gridHistory.AutoGenerateColumns = false;
        }

        private void tsbNewHistory_Click(object sender, EventArgs e)
        {
            ShowHistoryDetail(0);
        }

        private void ShowHistoryDetail(int historyId)
        {
            FormHistoryDetail historyDetail = new FormHistoryDetail();
            historyDetail.Tag = historyId;  //new history
            historyDetail.Owner = this;
            historyDetail.ShowDialog();
            historyDetail.Dispose();
        }

        private void FormHistory_Load(object sender, EventArgs e)
        {
            BindHistories();
        }

        public void BindHistories()
        {
            var dtHistory = mHistoryRepository.GetAllHistories();
            gridHistory.DataSource = dtHistory;
        }

        private void gridHistory_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int historyId = Convert.ToInt32(gridHistory.CurrentRow.Cells["colId"].Value);
                ShowHistoryDetail(historyId);
            }            
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                int historyId = Convert.ToInt32(gridHistory.CurrentRow.Cells["colId"].Value);
                mHistoryRepository.DeleteHistory(historyId);

                BindHistories();
            }
        }

        private void gridHistory_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //var newColumn = gridHistory.Columns[e.ColumnIndex];
            //var oldColumn = gridHistory.SortedColumn;

            //newColumn.SortMode = DataGridViewColumnSortMode.Programmatic;

            //ListSortDirection direction = ListSortDirection.Descending;

            //if (null != oldColumn)
            //{
            //    if (newColumn.Equals(oldColumn))
            //    {
            //        direction = ListSortDirection.Ascending;
            //    }
            //    oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
            //}

            //gridHistory.Sort(newColumn, direction);
            //if (direction == ListSortDirection.Ascending)
            //{
            //    newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
            //}
            //else
            //{
            //    newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending;
            //}
        }

        private void gridHistory_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            gridHistory.DrawRowNumber(e);
        }


    }
}
